<template>
	<view class="container">
		<uni-list v-if="showStatus">
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'shop-filled'}" title="猪舍编码" :rightText="phouse.phouseCode" />
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'location-filled'}" title="猪舍类型"
				:rightText="phouseType" />
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'person-filled'}" title="负责人"
				:rightText="phouse.hmyStaff.staffName" />
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'person-filled'}" title="猪舍大小"
				:rightText="phouse.phouseSize + ' '" />
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'chat-filled'}" title="备注">
				<template v-slot:footer>
					<input type="textarea" v-model="phouse.remark"
						style="font-size: 12px;color: #999999;text-align: right;" />
				</template>
			</uni-list-item>
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'person-filled'}" title="温度传感器"
				:rightText="phouseWenshi" />
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'person-filled'}" title="湿度传感器"
				:rightText="phouseWenshi" />
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'person-filled'}" title="猪舍风扇"
				:rightText="phouseFans" />
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'person-filled'}" title="猪舍监控"
				:rightText="phouseMonitor" />
		</uni-list>
		<view class="button-group" v-if="showStatus">
			<button plain="ture" type="warn" @click="phouseDelete" style="width: 45%;">删 除</button>
			<button plain="ture"  type="primary" @click="phouseEdit" style="width: 45%;">修 改</button>
		</view>
		
		<!-- 修改 -->
		<view class="example" v-if="!showStatus">
		  <uni-forms ref="form" :model="phouseForm" :rules="rules" labelWidth="80px">
			<uni-forms-item label="猪舍编码" name="phouseCode" required>
			  <uni-easyinput v-model="phouseForm.phouseCode" placeholder="请输入猪舍编码" />
			</uni-forms-item>
		    <uni-forms-item label="猪舍类型" name="phouseType" required>
		      <uni-data-select v-model="phouseForm.phouseType" 
			    :placeholder="phouseType"
		      	:localdata="hmy_park_type"
				@change="change"
		        ></uni-data-select>
		    </uni-forms-item>
			<uni-forms-item label="负责人" name="phouseManager" required>
				<uni-data-select v-model="phouseForm.phouseManager" placeholder="请选择负责人"
					:localdata="staffNameList"></uni-data-select>
			</uni-forms-item>
		    <uni-forms-item label="猪舍大小" name="phouseSize" required>
		      <uni-easyinput v-model="phouseForm.phouseSize" placeholder="请输入猪舍大小" />
		    </uni-forms-item>
			<uni-forms-item label="猪舍备注">
				<uni-easyinput type="textarea" v-model="phouseForm.remark" placeholder="请输入猪舍备注" />
			</uni-forms-item>
		  </uni-forms>
		  <view class="edit-button-group">
		  	<button type="primary" @click="back" plain="true" style="width: 45%;">返 回</button>
		  	<button type="primary" @click="editPhouse('form')" plain="true" style="width: 45%;">提 交</button>
		  </view>
		</view>
	</view>
</template>

<script>
import {addPhouse, delPhousr, findCode, getPhouse, listPhouse, updatePhousr} from "@/api/hmy/phouse";
import {listStaff,listStaffName} from "@/api/hmy/staff";
	export default {
		data() {
			return {
				showStatus:true,
				phouseId: '',
				phouseCode: '',
				phouseThermostatic: '',
				phouseWetness: '',
				phouseWenshi:'',
				phouseFans: '',
				phouseMonitor: '',
				phouse: {
					hmyStaff: {
					  staffName: '' // 确保 staffName 存在并初始化为空字符串
					}
				},
				staffNameList:[],
				phouseForm:{},
				phouseType: '',
				hmy_park_type:[
					{ value: 1, text: '公猪舍'},
					{ value: 2, text: '母猪舍'},
					{ value: 3, text: '肉猪舍'}
				],
				hmy_presence_absence:[
					{ text: '有', value: 1},
					{ text: '无', value: 2}
				],
				sys_normal_disable:[
					{ text: '正常', value: 1},
					{ text: '停用', value: 2}
				],
				// 表单校验
				rules: {
					phouseCode: {
						rules: [{required: true, errorMessage: '猪舍编码不能为空'}]
					},
					phouseManager: {
						rules: [{required: true, errorMessage: '负责人不能为空'}]
					},
					phouseType: {
						rules: [{required: true, errorMessage: '猪舍类型不能为空'}]
					}
				}
			}
		},
		created() {
			listStaffName().then(response => {
				var staffAllList = response.data;
				staffAllList.forEach(item => {
					if (item.staffType == '1') {
						this.staffNameList.push({
							'text': item.staffName,
							'value': item.staffId
						})
					}
				})
			})
		},
		onLoad(option) { //option为object类型，会序列化上个页面传递的参数
			//console.log(option.id);
			this.phouseId = option.id;
			this.getPhouseInfo();
		},
		methods: {
			change(e) {
				this.single = e
			},
			//获取记录
			getPhouseInfo() {
				//显示加载框
				uni.showLoading({
					title:"加载中"
				})
				getPhouse(this.phouseId).then(response => {
					this.phouse = response.data;
					this.phouseForm = response.data;
					this.phouse = response.data;
					if (this.phouse.phouseType == 1) {
						this.phouseType = '公猪舍'
					} else if (this.phouse.phouseType == 2) {
						this.phouseType = '母猪舍'
					} else if (this.phouse.phouseType == 3) {
						this.phouseType = '肉猪舍'
					} 
					if (this.phouse.phouseWenshi == 1) {
						this.phouseWenshi = '有'
					} else if (this.phouse.phouseWenshi == 2) {
						this.phouseWenshi = '无'
					}
					if (this.phouse.phouseFans == 1) {
						this.phouseFans = '有'
					} else if (this.phouse.phouseFans == 2) {
						this.phouseFans = '无'
					}
					if (this.phouse.phouseMonitor == 1) {
						this.phouseMonitor = '有'
					} else if (this.phouse.phouseMonitor == 2) {
						this.phouseMonitor = '无'
					}
					//隐藏加载框
					uni.hideLoading();
				})
			},
			//删除
			phouseDelete() {
				console.log("delete", this.phouse.phouseId)
				this.$modal.confirm('是否确认删除进货编号为"' + this.phouse.phouseId + '"的数据项？').then(() => {
					return delPhousr(this.phouse.phouseId);
				}).then(response => {
					this.$modal.msgSuccess("删除成功");
					setTimeout(()=>{
						this.$tab.navigateTo('/pages/hmy/phouse/index');
					},1000)
				}).catch(()=>{
				});
			},

			//修改
			phouseEdit() {
				//显示加载框
				uni.showLoading({
					title:"加载中"
				})
				this.showStatus = !this.showStatus;
				//隐藏加载框
				uni.hideLoading();
			},
			
			//返回
			back(){
				this.showStatus = !this.showStatus
				this.getPhouseInfo();
			},
			
			//修改提交
			editPhouse(form) {
			  this.$refs[form].validate().then(res => {
			    updatePhousr(this.phouseForm).then(response => {
			      this.$modal.msgSuccess("修改成功");
				  setTimeout(()=>{
				  	this.$tab.navigateTo('/pages/hmy/phouse/index');
				  },1000)
				  
			    })
			  })
			}
		}
	}
</script>

<style>
	
  page {
  		background-color: #ffffff;
  	}
  
  	.button-group {
  		display: flex;
  		justify-content: space-around;
  		width:100%;
  		position:fixed;
  		bottom:5px;
  
  	}
  	
  	.example {
  	  padding: 15px;
  	  background-color: #fff;
  	}
  	.edit-button-group{
  		display: flex;
  		justify-content: space-around;
  	}
  </style>